package com.baymax.test.scene.demo

import android.app.Activity
import android.app.Application
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStarted
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.R
import com.baymax.test.scene.addButton
import com.baymax.test.scene.drawable.LineDrawable
import com.baymax.test.scene.newScrollContainer
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       ActivityCallBackScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/8/23 15:53
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class ActivityCallBackScene:BaseScene() {

    val p =object :Application.ActivityLifecycleCallbacks{
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d(TAG, "onActivityCreated: $activity")
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d(TAG, "onActivityStarted: $activity")
        }

        override fun onActivityResumed(activity: Activity) {
            Log.d(TAG, "onActivityResumed: $activity")
        }

        override fun onActivityPaused(activity: Activity) {
            Log.d(TAG, "onActivityPaused: $activity")                  }

        override fun onActivityStopped(activity: Activity) {
            Log.d(TAG, "onActivityStopped: $activity")
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            Log.d(TAG, "onActivitySaveInstanceState: $activity")
        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d(TAG, "onActivityDestroyed: $activity")
        }
    }
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val s = this

        return p1.newScrollContainer {
            addButton("register") {
                (requireSceneContext().applicationContext as Application).registerActivityLifecycleCallbacks(
                    p
                )

            }

            addButton("register") {
                launch {
                    ProcessLifecycleOwner.get().whenStarted {
                        Log.d(TAG, "whenStarted: called")
                    }
                }
            }

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        (requireSceneContext().applicationContext as Application).unregisterActivityLifecycleCallbacks(
            p
        )
    }
}